﻿#include "dxstdafx.h"
#include "ProvincePositionsIO.h"

// (_stTerrainIDInfo*)(m_list_OperatorPtr.back())->P = (*T)(vp)  普通赋值操作
#define PUSHBACK_ASSIGN(P, T)\
[this](const void* vp){ CHECK_RETURN(vp);\
		_stProvincePosition* cp = static_cast<_stProvincePosition*>(m_list_OperatorPtr.back());\
		CHECK_RETURN(cp);\
		const T* tp = static_cast<const T*>(vp);\
		CHECK_RETURN(tp);\
		cp->P.push_back(*tp); }\

CProvincePositionReaderHandle::CProvincePositionReaderHandle(std::unordered_map<DWORD, _stProvincePosition>* map)
	: BaseReaderHandler(map)
{
	m_map_OperatorFunction = {
		{ L"position", PUSHBACK_ASSIGN(position, double) },
		{ L"rotation", PUSHBACK_ASSIGN(rotation, double) },
		{ L"height", PUSHBACK_ASSIGN(height, double) },
	};
}

bool CProvincePositionReaderHandle::HandleDouble(const std::wstring& key, double d)
{
	return OperatorFunction(key, &d);
}

bool CProvincePositionReaderHandle::HandleStartObj(const std::wstring& key)
{
	if (!key.empty())
	{
		CHECK_RE_FALSE(m_Container);
		_stProvincePosition st;
		DWORD dwKey = _wtoi(key.c_str());
		auto iterInsert = m_Container->insert(std::make_pair(dwKey, st));
		m_list_OperatorPtr.push_back(&iterInsert.first->second);
	}
	return true;
}

bool CProvincePositionReaderHandle::HandleEndObj()
{
	if (!m_list_OperatorPtr.empty())
	{
		m_list_OperatorPtr.pop_back();
	}
	return true;
}
